package com.wk.code.wkserver.service.impl;

import com.wk.code.wkserver.constant.Constant;
import com.wk.code.wkserver.constant.NumberConstant;
import com.wk.code.wkserver.entity.SysConfig;
import com.wk.code.wkserver.mapper.UserMapper;
import com.wk.code.wkserver.response.admin.*;
import com.wk.code.wkserver.service.HomeStatisticsService;
import com.wk.code.wkserver.service.ISysConfigService;
import com.wk.code.wkserver.utils.DataUtils;
import com.wk.code.wkserver.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author wq
 * @date 2021-10-16 16:42
 */
@Service
public class HomeStatisticsServiceImpl implements HomeStatisticsService {

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private UserMapper userMapper;

    @Override
    public AdminHomeResponse homeStatistics() {
        AdminHomeResponse response = new AdminHomeResponse();
        AdminHomeResponse statistics = userMapper.selectHomeStatistics();
        BeanUtils.copyProperties(statistics, response);
        //设置平台总收入
        SysConfig sysConfig = sysConfigService.selectByFieldValue(Constant.SETTLED_RATIO);
        if (Objects.nonNull(sysConfig)) {
            response.setTotalIncomeAmount(statistics.getTotalTradeAmount().multiply(new BigDecimal("0.2")));
            response.setTodayIncomeAmount(statistics.getTodayTradeAmount().multiply(new BigDecimal("0.2")));
            response.setYesterdayIncomeAmount(statistics.getYesterdayTradeAmount().multiply(new BigDecimal("0.2")));
        }
        //设置用户占比
        response.setDemandUserRatio(DataUtils.calcBigDecimal(statistics.getDemandUserNum(), statistics.getTotalNum()));
        response.setTechUserRatio(DataUtils.calcBigDecimal(statistics.getTechUserNum(), statistics.getTotalNum()));
        response.setVisitorUserRatio(DataUtils.calcBigDecimal(statistics.getTodayVisitorNum(), statistics.getTotalNum()));
        response.setYesterdayVisitorRatio(DataUtils.calcBigDecimal(statistics.getYesterdayVisitorNum(), statistics.getTotalNum()));
        return response;
    }

    @Override
    public AdminHomeChartResponse recentlyChart(Integer type) {
        List<String> sevenDate = DateUtils.getSevenDate();
        AdminHomeChartResponse response = new AdminHomeChartResponse();
        response.setDateTime(sevenDate);
        ArrayList<Integer> nums = new ArrayList<>();
        if (type.equals(NumberConstant.ONE)) {
            sevenDate.forEach(date -> {
                Integer registerNum = userMapper.selectRegisterNum(date);
                nums.add(registerNum);
            });

        } else {
            sevenDate.forEach(date -> {
                Integer visitorNum = userMapper.selectVisitorNum(date);
                nums.add(visitorNum);
            });
        }
        response.setNum(nums);
        return response;
    }


    @Override
    public AdminUserRatioResponse userRatio() {
        return userMapper.selectUserRatio();
    }

    @Override
    public AdminHomeMoneyResponse tradeRatio() {
        List<String> sevenDate = DateUtils.getSevenDate();
        AdminHomeMoneyResponse response = new AdminHomeMoneyResponse();
        //查询下单统计
        AdminHomeTradeResponse tradeResponse = new AdminHomeTradeResponse();
        tradeResponse.setDateTime(sevenDate);
        ArrayList<BigDecimal> nums = new ArrayList<>();
        for (String date : sevenDate) {
            BigDecimal tradeAmount = userMapper.selectTradeRatio(date);
            nums.add(tradeAmount);
        }
        tradeResponse.setNum(nums);
        response.setAdminHomeTradeResponse(tradeResponse);

        //查询收益统计
        AdminHomeIncomeResponse incomeResponse = new AdminHomeIncomeResponse();
        incomeResponse.setDateTime(sevenDate);
        ArrayList<BigDecimal> amounts = new ArrayList<>();
        for (String date : sevenDate) {
            BigDecimal tradeAmount = userMapper.selectTradeRatio(date);
            amounts.add(tradeAmount.multiply(new BigDecimal("0.2")));
        }
        incomeResponse.setNum(amounts);
        response.setAdminHomeIncomeResponse(incomeResponse);
        return response;
    }


}
